<template>
	<view class="home">
		<view class="shibie-item"  @click="toshibie">
			<image src="/static/1.png" mode="aspectFill" class="img" @click="toshibie"></image>
			<view class=""  @click="toshibie">
				动物识别
			</view>
		</view>
		<view class="shibie-item sweep"  @click="scanCodeHandler">
			<image src="/static/1.png" mode="aspectFill" class="img"></image>
			<view class="">
				扫一扫
			</view>
		</view>
	</view>
</template>

<script>
	import {
		pathToBase64
	} from '../../tools/index'
	export default {
		data() {
			return {
				base64Image: '',
				result: '',
				access_token: '',
			}
		},
		onLoad() {
			
		},
		methods: {
			toshibie(){
				if(uni.getStorageSync('token')){
					uni.navigateTo({
						url:'/pages/demo/index'
					})
				}else{
					uni.showModal({
						title:'提示',
						content: '请先登录在进行识别动物?',
						success: function (res) {
							if (res.confirm) {
								uni.reLaunch({
									url:'/pages/login/login'
								})
							} else if (res.cancel) {
								
							}
						}
					});
				}
			},
			scanCodeHandler(){
				let that = this;
				if(uni.getStorageSync('token')) {
					// 调起条码扫描
					uni.scanCode({
						onlyFromCamera: true, // 是否只能从相机扫码，不允许从相册选择图片
						scanType: ['qrCode'], // 扫码类型 qrCode二维码
						autoZoom: false, 	  // 是否启用自动放大，默认启用
						success: function (res) {
							console.log(`扫码结果：${JSON.stringify(res,null,2)}`);
							that.parseQRCode(res);
						},
						fail: function (err) {
							console.log(`错误：${err}`);
						},
						complete: function (data) {
							console.log('擦屁股！');
						}
					});
				} else {
					uni.showModal({
						title:'提示',
						content: '请先登录在进行扫一扫?',
						success: function (res) {
							if (res.confirm) {
								uni.reLaunch({
									url:'/pages/login/login'
								})
							} else if (res.cancel) {
								
							}
						}
					});
				}
			},
			parseQRCode(data){
				uni.vibrateShort();	// 震动提示
				uni.navigateTo({
					url: '/pages/scanCode/index?result=' + encodeURIComponent(data.result)
				});
				// uni.showToast({title: data.result})	// 弹出提示
			},
			// chooseImage() {
			// 	let that = this;
			// 	uni.chooseImage({
			// 		count: 1,
			// 		sizeType: ['original', 'compressed'],
			// 		sourceType: ['album', 'camera'],
			// 		success: (res) => {
			// 			const tempFilePaths = res.tempFilePaths[0];
			// 			pathToBase64(tempFilePaths)
			// 				.then(base64 => {
			// 					console.log("转换成功==>base64", base64)
			// 					that.base64Image = base64;
			// 					this.uploadImage();
			// 				})
			// 				.catch(error => {
			// 					console.error("转换失败==>", error)
			// 				})
			// 		}
			// 	});
			// },
			// uploadImage() {
			// 	const params = {
			// 		image: this.base64Image,
			// 		baike_num: '10',
			// 		top_num: '10',
			// 		'access_token': '24.ba393f17a3ce2693345aedc4111c17d1.2592000.1722239336.282335-35389279'
			// 	};
			
			// 	const header = {
			// 		'Content-Type': 'application/x-www-form-urlencoded',
			// 	};
			// 	let that = this
			// 	uni.request({
			// 		url: 'https://aip.baidubce.com/rest/2.0/image-classify/v1/animal',
			// 		method: 'POST',
			// 		data: params,
			// 		header: header,
			// 		success: (res) => {
			// 			if (res.data && res.data.result) {
			// 				this.result = res.data.result;
			// 				console.log(res)
			// 				if(uni.getStorageSync('historyList')){
			// 					let list = uni.getStorageSync('historyList')
			// 					list.push({
			// 						imgurl:that.base64Image,
			// 						result:res.data.result,
			// 						isChecked:false,
			// 						createTime:that.getDate()
			// 					})
			// 					uni.setStorageSync('historyList',list)
			// 					uni.navigateTo({
			// 						url:'/pages/detail/index'
			// 					})
			// 				}else{
			// 					let list =[]
			// 					list.push({
			// 						imgurl:that.base64Image,
			// 						result:res.data.result,
			// 						isChecked:false,
			// 						createTime:that.getDate()
			// 					})
			// 					uni.setStorageSync('historyList',list)
			// 					uni.navigateTo({
			// 						url:'/pages/detail/index'
			// 					})
			// 				}
							
			// 			} else {
			// 				this.result = '无法识别';
			// 			}
			// 		},
			// 		fail: () => {
			// 			this.result = '请求失败';
			// 		}
			// 	});
			// },
			// getDate(){
			// 	 var now = new Date();
			// 	    var year = now.getFullYear();
			// 	    var month = now.getMonth() + 1; // 月份是从0开始的，所以需要+1
			// 	    var day = now.getDate();
			// 	    var hours = now.getHours();
			// 	    var minutes = now.getMinutes();
			// 	    var seconds = now.getSeconds();
				 
			// 	    // 补零操作，确保月、日、时、分、秒都是两位数
			// 	    month = (month < 10 ? "0" : "") + month;
			// 	    day = (day < 10 ? "0" : "") + day;
			// 	    hours = (hours < 10 ? "0" : "") + hours;
			// 	    minutes = (minutes < 10 ? "0" : "") + minutes;
			// 	    seconds = (seconds < 10 ? "0" : "") + seconds;
				 
			// 	    return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
			// },
			// encodeParams() {
			// 	// 使用urlencode后的数据创建请求体
			// 	const params = {
			// 		image: this.base64Image,
			// 		image_type: 'BASE64'
			// 	};
			// 	return Object.keys(params).map((key) => {
			// 		return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
			// 	}).join('&');
			// }
					
		}
	}
</script>

<style scoped lang="scss">
	.home{
		background-color: #f7f7f7;
		height: 100vh;
		padding-top: 60rpx;
		box-sizing: border-box;
	}
	.shibie-item{
		box-sizing: border-box;
		width: calc(100vw - 60rpx);
		margin:0 30rpx;
		border-radius: 20rpx;
		padding: 30rpx 0;
		display: flex;
		align-items: center;
		flex-direction: column;
		font-size: 30rpx;
		font-weight: 700;
		background-color: #ffffff;
		box-shadow: 3px 5px 5px 0px rgba(0, 0, 0, 0.35);
		.img{
			width: 200rpx;
			height: 200rpx;
			margin-bottom: 16rpx;
		}
		&.sweep {
			margin-top: 40rpx;
		}
	}
</style>
